package com.kai.admin.security;

import com.kai.admin.security.constants.SecurityConstants;
import com.kai.admin.security.filter.CaptchaAuthenticationFilter;
import com.kai.admin.security.filter.UsernameAuthenticationFilter;
import com.kai.admin.security.provider.UsernameAuthenticationProvider;
import com.kai.auth.config.SecurityConfigurerAdapter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.crypto.factory.PasswordEncoderFactories;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;

/**
 * <p>
 * Spring Security 配置类
 * </p>
 *
 * @author weijk
 * @since 2023/7/11
 */
@Configuration
public class SecurityConfig extends SecurityConfigurerAdapter {

    @Autowired
    private UsernameAuthenticationProvider usernameAuthenticationProvider;

    @Autowired
    private AuthenticationFailureHandler authenticationFailureHandler;

    @Autowired
    private AuthenticationSuccessHandler authenticationSuccessHandler;

    @Autowired
    private LogoutSuccessHandler logoutSuccessHandler;

    @Autowired
    private CaptchaAuthenticationFilter captchaAuthenticationFilter;

    @Bean
    public PasswordEncoder passwordEncoder() {
        return PasswordEncoderFactories.createDelegatingPasswordEncoder();
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) {
        auth.authenticationProvider(usernameAuthenticationProvider);
    }

    @Override
    public void configure(HttpSecurity http) throws Exception {
        super.configure(http);
        http.addFilterBefore(captchaAuthenticationFilter, UsernamePasswordAuthenticationFilter.class)
                .addFilterBefore(usernameAuthenticationFilter(), UsernamePasswordAuthenticationFilter.class)
                .logout().logoutUrl(SecurityConstants.LOGOUT_URL).logoutSuccessHandler(logoutSuccessHandler);
    }

    private AbstractAuthenticationProcessingFilter usernameAuthenticationFilter() throws Exception {
        UsernameAuthenticationFilter authenticationFilter = new UsernameAuthenticationFilter(new AntPathRequestMatcher(SecurityConstants.LOGIN_URL));
        return setRequirement(authenticationFilter);
    }

    private AbstractAuthenticationProcessingFilter setRequirement(AbstractAuthenticationProcessingFilter authenticationFilter) throws Exception {
        authenticationFilter.setAuthenticationManager(super.authenticationManager());
        authenticationFilter.setAuthenticationSuccessHandler(authenticationSuccessHandler);
        authenticationFilter.setAuthenticationFailureHandler(authenticationFailureHandler);
        return authenticationFilter;
    }
}
